<!DOCTYPE html>
<!--
  The MIT License (MIT)

  Copyright (c) 2017 Tarek Sherif

  Permission is hereby granted, free of charge, to any person obtaining a copy of
  this software and associated documentation files (the "Software"), to deal in
  the Software without restriction, including without limitation the rights to
  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  the Software, and to permit persons to whom the Software is furnished to do so,
  subject to the following conditions:

  The above copyright notice and this permission notice shall be included in all
  copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-->
<html>
<head>
    <title>PicoGL.js: 3D Textures</title>
    <script src="utils/gl-matrix.js"></script>
    <script src="../build/picogl.js"></script>
    <script src="utils/utils.js"></script>
    <script src="utils/noise3D.js"></script>
    <link rel="stylesheet" href="../site/css/picogl-example.css"> 
</head>
<body>
    <div id="example-title">
        PicoGL.js Example: 3D Textures
        <div>
            <a href="https://github.com/tsherif/picogl.js/blob/master/examples/3Dtexture.html">Source code</a>
        </div>
    </div>
    <canvas id="gl-canvas"></canvas>
    <script type="x-shader/vs" id="vertex-draw">
        #version 300 es

        layout(location=0) in vec4 position;
        
        uniform mat4 uMVP;
        
        out vec3 vUV;
        void main() {
            vUV = position.xyz + 0.5;
            gl_Position = uMVP * position;
            gl_PointSize = 2.0;
        }
    </script>
    <script type="x-shader/vf" id="fragment-draw">
        #version 300 es
        precision highp float;
        precision lowp sampler3D;

        in vec3 vUV;

        uniform sampler3D tex;
        uniform float uTime;

        out vec4 fragColor;
        void main() {
            float alpha = texture(tex, vUV + vec3(0.0, 0.0, uTime)).r * 0.03;
            fragColor = vec4(fract(vUV) * alpha, alpha);
        }
    </script>
    <script type="text/javascript">
        utils.addTimerElement();

        if (!utils.testWebGL2()) {
            console.error("WebGL 2 not available");
            document.body.innerHTML = "This example requires WebGL 2 which is unavailable on this system."
        }

        var canvas = document.getElementById("gl-canvas");
        canvas.width = window.innerWidth;
        canvas.height = window.innerHeight;
        
        var app = PicoGL.createApp(canvas)
        .clearColor(0.0, 0.0, 0.0, 1.0)
        .blend()
        .blendFunc(PicoGL.ONE, PicoGL.ONE_MINUS_SRC_ALPHA);

        var timer = app.createTimer();

        // SET UP PROGRAM
        var vsSource =  document.getElementById("vertex-draw").text.trim();
        var fsSource =  document.getElementById("fragment-draw").text.trim();
        var program = app.createProgram(vsSource, fsSource);

        // CREATE POINT CLOUD
        var DIMENSIONS = 128;
        var INCREMENT = 1 / DIMENSIONS;

        var positionData = new Float32Array(DIMENSIONS * DIMENSIONS * DIMENSIONS * 3);

        var positionIndex = 0;
        var x = -0.5;
        for (var i = 0; i < DIMENSIONS; ++i) {
            var y = -0.5;
            for (var j = 0; j < DIMENSIONS; ++j) {
                var z = -0.5;
                for (var k = 0; k < DIMENSIONS; ++k) {
                    positionData[positionIndex++] = x;
                    positionData[positionIndex++] = y;
                    positionData[positionIndex++] = z;
                    z += INCREMENT;
                }
                y += INCREMENT;
            }
            x += INCREMENT;
        }

        var positions = app.createVertexBuffer(PicoGL.FLOAT, 3, positionData)

        var pointArray = app.createVertexArray()
        .vertexAttributeBuffer(0, positions);

        // CREATE 3D TEXTURE
        var TEXTURE_DIMENSIONS = 16;
        var textureData = new Uint8Array(TEXTURE_DIMENSIONS * TEXTURE_DIMENSIONS * TEXTURE_DIMENSIONS);
        var textureIndex = 0;
        for (var i = 0; i < TEXTURE_DIMENSIONS; ++i) {
            for (var j = 0; j < TEXTURE_DIMENSIONS; ++j) {
                for (var k = 0; k < TEXTURE_DIMENSIONS; ++k) {
                    var val = snoise([i, j, k]) * 255
                    textureData[textureIndex++] = val;
                }
            }
        }

        var texture = app.createTexture3D(textureData, TEXTURE_DIMENSIONS, TEXTURE_DIMENSIONS, TEXTURE_DIMENSIONS, { format: PicoGL.RED });

        // UNIFORM DATA
        var projMatrix = mat4.create();
        mat4.perspective(projMatrix, Math.PI / 2, canvas.width / canvas.height, 0.1, 10.0);

        var viewMatrix = mat4.create();
        var eyePosition = vec3.fromValues(1, 1, 1);
        mat4.lookAt(viewMatrix, eyePosition, vec3.fromValues(0, 0, 0), vec3.fromValues(0, 1, 0));

        var mvpMatrix = mat4.create();
        mat4.multiply(mvpMatrix, projMatrix, viewMatrix);
        

        window.onresize = function() {
            app.resize(window.innerWidth, window.innerHeight);

            mat4.perspective(projMatrix, Math.PI / 2, app.width / app.height, 0.1, 10.0);
            mat4.multiply(mvpMatrix, projMatrix, viewMatrix);            
        }

        // SET UP DRAW CALL
        var drawCall = app.createDrawCall(program, pointArray, PicoGL.POINTS)
        .texture("tex", texture)
        .uniform("uMVP", mvpMatrix);

        var startTime = performance.now();
        function draw() {
            if (timer.ready()) {
                utils.updateTimerElement(timer.cpuTime, timer.gpuTime);
            }

            timer.start();

            drawCall.uniform("uTime", (performance.now() - startTime) / 1000);

            app.clear();
            drawCall.draw();
            
            timer.end();

            requestAnimationFrame(draw);
        }

        requestAnimationFrame(draw);
            

    </script>
    <a href="https://github.com/tsherif/picogl.js" id="github-ribbon"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://camo.githubusercontent.com/365986a132ccd6a44c23a9169022c0b5c890c387/68747470733a2f2f73332e616d617a6f6e6177732e636f6d2f6769746875622f726962626f6e732f666f726b6d655f72696768745f7265645f6161303030302e706e67" alt="Fork me on GitHub" data-canonical-src="https://s3.amazonaws.com/github/ribbons/forkme_right_red_aa0000.png"></a>
    <script src="../site/js/iframe.js"></script>
</body>
</html>
